<template>
  <div class="container">
    <el-card class="heaer-card" :body-style="{ padding: 10 }">
      <el-form :inline="true" :model="queryParam" class="demo-form-inline" size="medium">
        <el-form-item label="操作人">
          <el-input v-model="queryParam.userName" placeholder="请输入操作人" clearable></el-input>
        </el-form-item>
        <el-form-item label="操作日期">
          <el-date-picker v-model="loginDate" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作模块">
          <el-input v-model="queryParam.model" placeholder="请输入操作模块" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="body-card" :body-style="{ padding: 10 }">
      <el-table :data="tableData" border style="width: 100%" height="58vh">
        <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
        <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
          :width="item.width">
          <template #default="scope">
            <div>{{ scope.row[item.prop] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="180">
          <template slot-scope="{ row }">
            <el-button icon="el-icon-view" link size="mini" @click="handleShow(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="foot-card" :body-style="{ padding: 10 }">
      <pagination ref="pagination" :totalCount="totalCount" @pagination="pagination"></pagination>
    </el-card>
    <opt-log-dialog ref="optLogDialog" @ok="loadData" />
  </div>
</template>

<script>
import { pageList } from '@/api/sys/optLog'
import optLogDialog from './dialog/optLogDialog.vue'
import moment from 'moment'

export default {
  name: 'optLog',
  components: { optLogDialog },
  data() {
    return {
      totalCount: 0, //总数
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam: {},
      loginDate: [],
      tableData: [],
      tableProps: [
        { title: '操作日期', prop: 'optTime', width: '', checked: true },
        { title: '操作人', prop: 'userName', width: '', checked: true },
        { title: 'IP', prop: 'ip', width: '', checked: true },
        { title: '操作模块', prop: 'model', width: '', checked: true },
        { title: '操作内容', prop: 'content', width: '', checked: true },
      ],
    }
  },

  mounted() {
    this.loadData()
  },

  methods: {
    // 授权
    handlePower(row) {
      this.$refs.powerMenu.openDialog(row)
    },
    onSubmit() {
      this.$refs.pagination.resetPagination()
    },
    reset() {
      this.loginDate = []
      this.queryParam = {}
      this.$refs.pagination.resetPagination()
    },
    loadData() {
      if (this.loginDate != null && this.loginDate.length > 0) {
        this.queryParam.beginLoginTime = moment(this.loginDate[0]).format('YYYY-MM-DD 00:00:00')
        this.queryParam.endLoginTime = moment(this.loginDate[1]).format('YYYY-MM-DD 23:59:59')
      } else {
        this.queryParam.beginLoginTime = null
        this.queryParam.endLoginTime = null
      }
      var param = Object.assign(this.queryParam, this.searchModel)
      pageList(param)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records
            this.totalCount = res.data.data.total
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    pagination(val) {
      this.searchModel.pageNo = val.page
      this.searchModel.pageSize = val.limit
      this.loadData()
    },
    doSearch(value) {
      this.queryParam = this.ArraryToObject(value)
      this.loadData()
    },
    handleShow(row) {
      this.$refs.optLogDialog.show(row)
    },
  },
}
</script>

<style lang="less" scoped>
.el-form-item {
  margin-bottom: 0 !important;
}

.container {
  height: 100%;

  .body-card {
    height: calc();
  }
}
</style>
